import { IMG_HOST, PORT_PAT, URL_PAT } from '@/utils/netConf'

export interface Message {
  sender: number,
  receiver: number,
  msg: string
}

export interface Breakpoint {
  id?: number,
  pid: number,
  idx: number,
  subject: string,
  category: string
}

export interface Header {
  name: string,
  len: number
}

export interface Question {
  id: number,
  year: string,
  number: number,
  question: string,
  tag: string,
  answer: string,
  type: string,
  subject: string,
  choice: string
}

export interface QusPad {
  id: number,
  year: string,
  number: number,
  question: string,
  tag: string,
  type: string,
  subject: string,
  choice: string
}
interface Answer {
  content: string,
  author: string
}

// Long id;
// String year;
// Integer number;
// String question;
// String tag;
// String answer;
// String type;
// String subject;
// String choice;
export interface QusItem {
  id: number,
  year: string,
  number: number,
  question: string,
  tag: string[],
  type: string,
  subject: string,
  choice: string,
  answer: Answer[],
  acc: number,
  try: number
}

export interface QusPair {
  rt: string,
  ct: string
}

export interface AnswerPad {
  id: number,
  answer: string,
  choice: string
}

export function createDefaultQuestion () {
  const data:Question = {
    id: 0,
    year: '',
    number: 0,
    question: '',
    tag: '',
    answer: '',
    type: '',
    subject: '',
    choice: ''
  }
  return data
}

export function createDefaultQusPad () {
  const data:QusPad = {
    id: 0,
    year: '',
    number: 0,
    question: '',
    tag: '',
    type: '',
    subject: '',
    choice: ''
  }
  return data
}

export function createDefaultAnswerPad () {
  const data:AnswerPad = {
    id: 0,
    answer: '',
    choice: ''
  }
  return data
}

export function createDefaultQusItem () {
  const data:QusItem = {
    id: 0,
    year: '',
    number: 0,
    question: '',
    tag: [],
    type: '',
    subject: '',
    choice: '',
    answer: [],
    acc: 0,
    try: 0
  }
  return data
}

export interface KillRecord {
  select: string,
  status: number,
  pid: number,
  created: number
}

export interface SolveLog {
  subject?: string,
  uid: number,
  pid: number,
  status: number,
  created: number
}

function randInt (lowBound: number, upBound: number): number {
  const r = Math.random() * (upBound - lowBound)
  return Math.floor(r) + lowBound
}

function shuffle (num: number[]): number[] {
  const arr:number[] = num.map(x => x)
  for (let i = arr.length; i > 1; i--) {
    const j = randInt(0, i)
    // swap
    const t = arr[j]
    arr[j] = arr[i - 1]
    arr[i - 1] = t
  }
  return arr
}

export function randSelect (num: number[], n: number): number[] {
  const arr = shuffle(num)
  n = n <= arr.length ? n : arr.length
  const ans: number[] = []
  for (let i = 0; i < n; i++) {
    ans.push(arr[i])
  }
  return ans
}

export interface Priority {
  admin: number,
  editor: number
}

export interface Collect {
  pid: number,
  created: number,
  category: number
}

export function transUrl (origin: string): string {
  origin = origin.replace(PORT_PAT, '')
  origin = origin.replace(URL_PAT, IMG_HOST)
  return origin
}

export interface MessageSend {
  msg: string,
  senderName: string,
  receiver: number,
  ref?: number
}

export interface CommentSend {
  pid: number,
  sender: string,
  content: string,
  created: number
}

export interface ReplySend {
  cid: number,
  refId: number,
  sender: string,
  receiver: string,
  rid: number,
  content: string,
  created: number
}

export interface CommentDs {
  id: number,
  cid: number,
  refId: number,
  pid: number,
  sender: string,
  sid: number,
  receiver: string,
  rid: number,
  content: string,
  time: string,
  like: number,
  dislike: number,
  action: number,
  showReply: boolean,
  showConversation: boolean,
  canDelete: boolean,
  initLoadReply: boolean,
  replyLoadStatus: boolean,
  conversations: CommentDs[]
}

export function createCommentDs (): CommentDs {
  return {
    id: 0,
    cid: 0,
    refId: 0,
    pid: 0,
    sender: '',
    sid: 0,
    receiver: '',
    rid: 0,
    content: '',
    time: '0',
    like: 0,
    dislike: 0,
    action: 0,
    showReply: false,
    showConversation: false,
    canDelete: false,
    initLoadReply: false,
    replyLoadStatus: false,
    conversations: []
  }
}

export interface HistoryAnswer {
  id: number,
  pid: number,
  uid: number,
  created: number,
  choice: string
}

export interface RestPass {
  email: string,
  passwd: string,
  validCode: string
}

export interface AnswerReq {
  content: string,
  pid: number,
  aid: number
}
